package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1124 {
    public long startExecuteTime = System.currentTimeMillis();


    /*
     * @param 此题目参考了别人代码
     * 这是因为问题情况较为复杂
     * 未来需要再次复习此道题目
     * 这种判断前缀和长度最大，前缀和余某数最大的题目一般都需要使用map存储前者下标
     * 套路啊，还有一定的数学分析！
     * @return:
     */
    class Solution {
        public int longestWPI(int[] hours) {
            int cur = 0;
            int res = 0;
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < hours.length; i++) {
                if (hours[i] > 8) {
                    cur++;
                } else {
                    cur--;
                }
                if (cur > 0) {
                    res = i + 1;
                } else {
                    if (!map.containsKey(cur)) {
                        map.put(cur, i);
                    }
                    if (map.containsKey(cur - 1)) {
                        res = Math.max(res, i - map.get(cur - 1));
                    }
                }
            }
            return res;
        }
    }

    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC1124 an = new LC1124();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
